package com.ddwl.search.controller;

import com.ddwl.common.annotation.Auth;
import com.ddwl.common.constant.BaseEnum;
import com.ddwl.common.controller.BaseController;
import com.ddwl.schema.bo.base.PageBo;
import com.ddwl.schema.vo.base.PageVo;
import com.ddwl.schema.vo.base.RestVo;
import com.ddwl.schema.vo.goods.ItemVo;
import com.ddwl.schema.vo.search.EsItemVo;
import com.ddwl.search.esrepository.domain.EsItem;
import com.ddwl.schema.bo.search.GoodsSearchBo;
import com.ddwl.search.service.EsProductServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 搜索商品管理Controller
 * Created by macro on 2018/6/19.
 */
@RestController
@Api(tags = "搜索商品管理")
@RequestMapping("/esProduct")
public class EsProductController extends BaseController {

    @Autowired
    private EsProductServiceImpl esProductService;


    @ApiOperation(value = "导入所有数据库中商品到ES")
    @PostMapping(value = "/importAll")
    @Auth(product = {BaseEnum.ProductEnum.API, BaseEnum.ProductEnum.CONSOLE})
    public RestVo<Integer> importAllList() {
        int count = esProductService.importAll();
        return RestVo.ok(count);
    }

    @ApiOperation(value = "根据id删除商品")
    @Auth(product = {BaseEnum.ProductEnum.API, BaseEnum.ProductEnum.CONSOLE})
    @DeleteMapping(value = "/delete/{id}")
    public RestVo<Object> delete(@PathVariable String id) {
        esProductService.delete(id);
        return RestVo.ok(null);
    }

    @ApiOperation(value = "根据id批量删除商品")
    @Auth(product = {BaseEnum.ProductEnum.API, BaseEnum.ProductEnum.CONSOLE})
    @PostMapping(value = "/delete/batch", produces = MediaType.APPLICATION_JSON_VALUE)
    public RestVo<Object> delete(@RequestParam("ids") List<String> ids) {
        esProductService.delete(ids);
        return RestVo.ok(null);
    }

    @ApiOperation(value = "同步商品信息")
    @Auth(product = {BaseEnum.ProductEnum.API, BaseEnum.ProductEnum.CONSOLE})
    @PostMapping(value = "/syncCreate/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
    public RestVo<EsItem> create(@PathVariable String id) {
        return RestVo.ok( esProductService.create(id));
    }

    @ApiOperation(value = "创建商品信息")
    @Auth(product = {BaseEnum.ProductEnum.API, BaseEnum.ProductEnum.CONSOLE})
    @PostMapping(value = "/create", produces = MediaType.APPLICATION_JSON_VALUE)
    public RestVo<ItemVo> create(@RequestBody ItemVo itemVo) {
        return RestVo.ok( esProductService.create(itemVo));
    }


    @ApiOperation(value = "商品搜索")
    @PostMapping(value = "/search", produces = MediaType.APPLICATION_JSON_VALUE)
    public RestVo<PageVo<EsItemVo>> search(@RequestBody PageBo<GoodsSearchBo> searchBo) {
        if(searchBo.getParam()==null){
            searchBo.setParam(new GoodsSearchBo());
        }
        //分销和商城默认查询平台商品
        if (BaseEnum.ProductEnum.FENXIAO.getName().equals(getProduct()) || BaseEnum.ProductEnum.MALL.getName().equals(getProduct())) {
            searchBo.getParam().setItemType("0");
        }
        return RestVo.ok(esProductService.search(searchBo));
    }


}
